package com.ztk.threadbasic.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.util.StringUtils;
import com.ztk.threadbasic.excel.exception.CustomReaderExcelException;
import com.ztk.threadbasic.excel.listener.CustomReadListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.util.StringUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Slf4j
public class EasyExcelDemo7 {
        public static void main(String[] args) throws CustomReaderExcelException{
            try {
                String fileName = "D:\\abc.tmp";
                File file = new File(fileName);

                FileInputStream fileInputStream = new FileInputStream(file);
                CustomReadListener customReadListener = new CustomReadListener();
//                ExcelReaderBuilder read = EasyExcel.read(fileName, CompanyVO.class, customReadListener);
                ExcelReaderBuilder read = EasyExcel.read(fileInputStream, CompanyVO.class, customReadListener);

                read.sheet(0).headRowNumber(4).doRead();
    
                String budgetAgency = customReadListener.getBudgetCom();
                String budgetDate = customReadListener.getBudgetDate();
                List<CompanyVO> companyVOList = customReadListener.getCompanyVOList();
    
                for (int i = 0; i < companyVOList.size(); i++) {
                    CompanyVO companyVO = companyVOList.get(i);
                    if (i != 0) {
                        String project = companyVO.getProject();
                        if (StringUtils.isEmpty(project)) {
                            CompanyVO companyVOPre = companyVOList.get(i - 1);
                            String projectPre = companyVOPre.getProject();
                            companyVO.setProject(projectPre);
                        }
                    }
                    companyVO.setBudgetAgency(budgetAgency);
                    companyVO.setBudgetDate(budgetDate);
                    log.info("第{}行数据为：{}", i + 4, companyVO);
                }
            } catch (Exception e) {
                log.info("excel读取异常!!!");
                log.info(e.getMessage(), e);
                throw new CustomReaderExcelException("excel读取异常!!!");
            } finally {
                log.info("excel读取结束!!!");
            }
        }

}


